// mock.js 文件
import { Random as _Random } from 'mockjs'
const Random = _Random
let dataList = []
for (let i = 0; i < 26; i++) {
    const template = {
        id: i,
        title: Random.title(5),
        name: Random.name(),
        images: Random.image('250x150'),
        category: Random.word(5),
        datatime: Random.datetime(),
        content: Random.word(30),
        status: true
    }
    dataList.push(template)
}

export default [
    // 分页
    {
        url: '/mock/articles/list',
        type: 'post',
        response: option => {
            let { page, pageSize, category, status } = option.body
            let newDataList = dataList
            if (status === true || status === false) {
                newDataList = newDataList.filter(v => v.status === status)
            }
            if (category != '') {
                newDataList = newDataList.filter(
                    v => v.category === category
                )
            }
            let newDataLists = newDataList.slice(
                (page - 1) * pageSize,
                page * pageSize
            )
            return {
                error: '',
                status: 1,
                data: {
                    tableData: newDataLists,
                    total: newDataList.length
                }
            }
        }
    },
    // 新增
    {
        url: '/mock/articles/add',
        type: 'post',
        response: option => {
            let data = option.body
            data.id = dataList.length
            dataList.push(data)
            return {
                error: '',
                status: 1,
                message: '发表文章成功'
            }
        }
    },
    // 删除
    {
        url: '/mock/articles/delete',
        type: 'post',
        response: option => {
            let data = option.body
            dataList = dataList.filter(v => v.id != data.id)
            return {
                error: '',
                status: 1,
                message: '删除文章成功'
            }
        }
    },
    // 修改
    {
        url: '/mock/articles/update',
        type: 'post',
        response: option => {
            let data = option.body
            for (var i in dataList) {
                if (dataList[i].id === data.id) {
                    dataList[i] = data
                }
            }
            return {
                error: '',
                status: 1,
                message: '发布文章成功'
            }
        }
    }
]
